package com.itboye.textileexpres.myset;

import java.util.Observable;
import java.util.Observer;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.presenter.GetYanzhengMaPresenter;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.TimeCount;

public class ActivityPersonPassword extends BaseActivity implements
		OnClickListener, Observer {
	ImageView back;
	TextView title;
	ImageView img_other;
	EditText old_password, new_password, again_password;
	TextView ok;
	MainPresenter mainPresenter;
	String oldpassword;
	String newpassword;
	String newpassword2;
	String uid;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_person_password;

	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		title.setText("修改密码");
		mainPresenter = new MainPresenter(this);
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.ok:
			oldpassword = old_password.getText().toString().trim();
			newpassword = new_password.getText().toString().trim();
			newpassword2 = again_password.getText().toString().trim();
			if ("".equals(oldpassword) || oldpassword == "") {
				toast("原密码不能为空");
				return;
			} else if ("".equals(newpassword) || newpassword == "") {
				toast("新密码不能为空");
				return;
			} else if ("".equals(newpassword2) || newpassword2 == "") {
				toast("请再输入一次新密码");
				return;
			} else if ((!"".equals(newpassword2) && !"".equals(newpassword))
					&& !newpassword.equals(newpassword2)) {
				toast("两次密码输入不一致");
			} else {
				showProgressDialog("密码更改中", true);
				mainPresenter
						.updatePasswordByPwd(uid, newpassword, oldpassword);

			}
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					MainPresenter.UpdatePswByPwdSuccessEvent)) {
				toast(entity.getData() + "");
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}
				finish();
			} else if (entity.getEventType().equals(
					MainPresenter.UpdatePswByPwdFailureEvent)) {
				toast(entity.getData() + "");
			}

		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
